/**
 * @Author: aesoper
 * @Description:
 * @File:  tracer
 * @Version: 1.0.0
 * @Date: 2020/7/24 14:33
 */

package tracer

import (
	"github.com/opentracing/opentracing-go"
	"github.com/uber/jaeger-client-go"
	"github.com/uber/jaeger-client-go/config"
	"github.com/uber/jaeger-lib/metrics/prometheus"
	"io"
)

type JaegerOptions struct {
	ServiceName string `mapstructure:"serviceName"`
	Addr        string `mapstructure:"addr"`
}

// 初始化
func InitializeJaeger(opts JaegerOptions) (opentracing.Tracer, io.Closer, error) {
	cfg := config.Configuration{
		ServiceName: opts.ServiceName,
		Sampler: &config.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},

		Reporter: &config.ReporterConfig{
			LogSpans:           true,
			LocalAgentHostPort: opts.Addr,
		},
	}

	metricsFactory := prometheus.New()

	tracer, closer, err := cfg.NewTracer(config.Metrics(metricsFactory))
	if err == nil {
		opentracing.SetGlobalTracer(tracer)
	}

	return tracer, closer, err
}
